import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreMemberConfigService extends BaseService<any> {
  private readonly logger = new Logger(CoreMemberConfigService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getLoginConfig
   * 对应 PHP: CoreMemberConfigService_core::getLoginConfig()
   * 逻辑类型: undefined - undefined
   */
  async getLoginConfig(site_id: number) {
    // 基于PHP真实逻辑: getLoginConfig
    // PHP原文: $info = ( new CoreConfigService() )->getConfig($site_id, 'LOGIN')[ 'value' ] ?? [];         return [             'is_username' => $info[ 'is_username'...
const info = ( this.coreConfigService ).getConfig(site_id, 'LOGIN')[ 'value' ] || [];
        return [
            is_username: info[ 'is_username' ] || 1, // 是否用户名密码登录
            is_mobile: info[ 'is_mobile' ] || 0, // 是否手机验证码登录
            is_auth_register: info[ 'is_auth_register' ] || 1, // 是否第三方自动注册
            is_force_access_user_info: info[ 'is_force_access_user_info' ] || 0, // 是否强制获取用户信息
            is_bind_mobile: info[ 'is_bind_mobile' ] || 0, // 是否强制绑定手机
            agreement_show: info[ 'agreement_show' ] || 0, // 政策协议是否展示
            bg_url: info[ 'bg_url' ] || '', // 背景图片
            desc: info[ 'desc' ] || '精选好物，购物优惠的省钱平台' // 描述
        ];
    }

    /**
     * 注册与登录设置
     * @param int site_id
     * @param data
     * @return true
     */
    async setLoginConfig(int site_id, data)
    {
        const config = [
            is_username: data[ 'is_username' ] || 1,// 是否用户名密码登录
            is_mobile: data[ 'is_mobile' ] || 0, // 是否手机验证码登录
            is_auth_register: data[ 'is_auth_register' ] || 1,// 是否第三方自动注册
            is_force_access_user_info: data[ 'is_force_access_user_info' ] || 0, // 是否强制获取用户信息
            is_bind_mobile: data[ 'is_bind_mobile' ] || 0, // 是否强制绑定手机
            agreement_show: data[ 'agreement_show' ] || 0, // 政策协议是否展示
            bg_url: data[ 'bg_url' ] || '', // 背景图片
            desc: data[ 'desc' ] || '' // 描述
        ];
        ( this.coreConfigService ).setConfig(site_id, 'LOGIN', config);
        return true;
    }

    /**
     * 获取会员设置
     * @param int site_id
     * @return */
    async getMemberConfig(int site_id)
    {
        const info = ( this.coreConfigService ).getConfig(site_id, 'MEMBER')[ 'value' ] || [];

        info[ 'form_id' ] = info[ 'form_id' ] || '';
        if(!!info[ 'form_id' ))) {
            const diy_form_model = this.diyFormService;
            const diy_form_count = diy_form_model.where([
                [ 'site_id', '=', site_id ],
                [ 'form_id', '=', info[ 'form_id' ] )
            )).count(];
            if (diy_form_count == 0) {
                info[ 'form_id' ] = '';
            }
  }

  /**
   * setLoginConfig
   * 对应 PHP: CoreMemberConfigService_core::setLoginConfig()
   * 逻辑类型: undefined - undefined
   */
  async setLoginConfig(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: setLoginConfig
    // PHP原文: $config = [             'is_username' => $data[ 'is_username' ] ?? 1,// 是否用户名密码登录             'is_mobile' => $data[ 'is_mobile' ] ?? 0, // 是否手机验证码登录  ...
const config = [
            is_username: data[ 'is_username' ] || 1,// 是否用户名密码登录
            is_mobile: data[ 'is_mobile' ] || 0, // 是否手机验证码登录
            is_auth_register: data[ 'is_auth_register' ] || 1,// 是否第三方自动注册
            is_force_access_user_info: data[ 'is_force_access_user_info' ] || 0, // 是否强制获取用户信息
            is_bind_mobile: data[ 'is_bind_mobile' ] || 0, // 是否强制绑定手机
            agreement_show: data[ 'agreement_show' ] || 0, // 政策协议是否展示
            bg_url: data[ 'bg_url' ] || '', // 背景图片
            desc: data[ 'desc' ] || '' // 描述
        ];
        ( this.coreConfigService ).setConfig(site_id, 'LOGIN', config);
        return true;
    }

    /**
     * 获取会员设置
     * @param int site_id
     * @return */
    async getMemberConfig(int site_id)
    {
        const info = ( this.coreConfigService ).getConfig(site_id, 'MEMBER')[ 'value' ] || [];

        info[ 'form_id' ] = info[ 'form_id' ] || '';
        if(!!info[ 'form_id' ))) {
            const diy_form_model = this.diyFormService;
            const diy_form_count = diy_form_model.where([
                [ 'site_id', '=', site_id ],
                [ 'form_id', '=', info[ 'form_id' ] )
            )).count(];
            if (diy_form_count == 0) {
                info[ 'form_id' ] = '';
            }
  }

  /**
   * getMemberConfig
   * 对应 PHP: CoreMemberConfigService_core::getMemberConfig()
   * 逻辑类型: undefined - undefined
   */
  async getMemberConfig(site_id: number) {
    // 基于PHP真实逻辑: getMemberConfig
    // PHP原文: $info = ( new CoreConfigService() )->getConfig($site_id, 'MEMBER')[ 'value' ] ?? [];          $info[ 'form_id' ] = $info[ 'form_id' ] ?? '';         i...
const info = ( this.coreConfigService ).getConfig(site_id, 'MEMBER')[ 'value' ] || [];

        info[ 'form_id' ] = info[ 'form_id' ] || '';
        if(!!info[ 'form_id' ))) {
            const diy_form_model = this.diyFormService;
            const diy_form_count = diy_form_model.where([
                [ 'site_id', '=', site_id ],
                [ 'form_id', '=', info[ 'form_id' ] )
            )).count(];
            if (diy_form_count == 0) {
                info[ 'form_id' ] = '';
            }
  }

  /**
   * setMemberConfig
   * 对应 PHP: CoreMemberConfigService_core::setMemberConfig()
   * 逻辑类型: undefined - undefined
   */
  async setMemberConfig(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: setMemberConfig
    // PHP原文: $config = [             'prefix' => $data[ 'prefix' ] ?? '',// 会员编码前缀             'length' => $data[ 'length' ] ?? 4,// 会员编码长度             'form_id' =...
const config = [
            prefix: data[ 'prefix' ] || '',// 会员编码前缀
            length: data[ 'length' ] || 4,// 会员编码长度
            form_id: data[ 'form_id' ] || '',// 万能表单id
        ];
        ( this.coreConfigService ).setConfig(site_id, 'MEMBER', config);
        return true;
    }

    /**
     * 获取成长值规则配置
     * @param int site_id
     * @return */
    async getGrowthRuleConfig(int site_id)
    {
        return ( this.coreConfigService ).getConfigValue(site_id, 'GROWTH_RULE');
    }

    /**
     * 配置成长值规则
     * @param int site_id
     * @param data
     * @return true
     */
    async setGrowthRuleConfig(int site_id, data)
    {
        ( this.coreConfigService ).setConfig(site_id, 'GROWTH_RULE', data);
        return true;
    }

    /**
     * 获取积分规则配置
     * @param int site_id
     * @return */
    async getPointRuleConfig(int site_id)
    {
        return ( this.coreConfigService ).getConfigValue(site_id, 'POINT_RULE');
    }

    /**
     * 配置积分规则
     * @param int site_id
     * @param data
     * @return true
     */
    async setPointRuleConfig(int site_id, data)
    {
        ( this.coreConfigService ).setConfig(site_id, 'POINT_RULE', data);
        return true;
    }

    /**
     * 获取会员提现设置
     * @param int site_id
     * @return */
    async getCashOutConfig(int site_id)
    {
        const config = ( this.coreConfigService ).getConfig(site_id, ConfigKeyDict.MEMBER_CASH_OUT)[ 'value' ] || [];
        return [
            is_open: config[ 'is_open' ] || '0',//是否启用提现
            transfer_type: config[ 'transfer_type' ] || [],//提现方式
            min: config[ 'min' ] || '0',//最低提现金额
//            max: '0',//最高提现金额
            rate: config[ 'rate' ] || '0',//手续费比率
            is_auto_verify: config[ 'is_auto_verify' ] || '0',  //是否自动审核
            is_auto_transfer: config[ 'is_auto_transfer' ] || '0',  //是否自动转账
        ];
    }

    /**
     * 会员提现配置
     * @param int site_id
     * @param data
     * @return true
     */
    async setCashOutConfig(int site_id, data)
    {
        //校验转账方式是否合法
        const transfer_type_list = array_keys(TransferDict.getTransferType()];
        if (array_diff(array_diff(data[ 'transfer_type' ), transfer_type_list), transfer_type_list)) throw new BusinessException('TRANSFER_TYPE_NOT_EXIST');
        foreach (transfer_type_list as key => item) {
            if (!in_[item, data[ 'transfer_type' ))) {
                unset(transfer_type_list[ key )];
            }
  }

  /**
   * getGrowthRuleConfig
   * 对应 PHP: CoreMemberConfigService_core::getGrowthRuleConfig()
   * 逻辑类型: undefined - undefined
   */
  async getGrowthRuleConfig(site_id: number) {
    // 基于PHP真实逻辑: getGrowthRuleConfig
    // PHP原文: return ( new CoreConfigService() )->getConfigValue($site_id, 'GROWTH_RULE');     }      /**      * 配置成长值规则      * @param int $site_id      * @param ar...
return ( this.coreConfigService ).getConfigValue(site_id, 'GROWTH_RULE');
    }

    /**
     * 配置成长值规则
     * @param int site_id
     * @param data
     * @return true
     */
    async setGrowthRuleConfig(int site_id, data)
    {
        ( this.coreConfigService ).setConfig(site_id, 'GROWTH_RULE', data);
        return true;
    }

    /**
     * 获取积分规则配置
     * @param int site_id
     * @return */
    async getPointRuleConfig(int site_id)
    {
        return ( this.coreConfigService ).getConfigValue(site_id, 'POINT_RULE');
    }

    /**
     * 配置积分规则
     * @param int site_id
     * @param data
     * @return true
     */
    async setPointRuleConfig(int site_id, data)
    {
        ( this.coreConfigService ).setConfig(site_id, 'POINT_RULE', data);
        return true;
    }

    /**
     * 获取会员提现设置
     * @param int site_id
     * @return */
    async getCashOutConfig(int site_id)
    {
        const config = ( this.coreConfigService ).getConfig(site_id, ConfigKeyDict.MEMBER_CASH_OUT)[ 'value' ] || [];
        return [
            is_open: config[ 'is_open' ] || '0',//是否启用提现
            transfer_type: config[ 'transfer_type' ] || [],//提现方式
            min: config[ 'min' ] || '0',//最低提现金额
//            max: '0',//最高提现金额
            rate: config[ 'rate' ] || '0',//手续费比率
            is_auto_verify: config[ 'is_auto_verify' ] || '0',  //是否自动审核
            is_auto_transfer: config[ 'is_auto_transfer' ] || '0',  //是否自动转账
        ];
    }

    /**
     * 会员提现配置
     * @param int site_id
     * @param data
     * @return true
     */
    async setCashOutConfig(int site_id, data)
    {
        //校验转账方式是否合法
        const transfer_type_list = array_keys(TransferDict.getTransferType()];
        if (array_diff(array_diff(data[ 'transfer_type' ), transfer_type_list), transfer_type_list)) throw new BusinessException('TRANSFER_TYPE_NOT_EXIST');
        foreach (transfer_type_list as key => item) {
            if (!in_[item, data[ 'transfer_type' ))) {
                unset(transfer_type_list[ key )];
            }
  }

  /**
   * setGrowthRuleConfig
   * 对应 PHP: CoreMemberConfigService_core::setGrowthRuleConfig()
   * 逻辑类型: undefined - undefined
   */
  async setGrowthRuleConfig(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: setGrowthRuleConfig
    // PHP原文: ( new CoreConfigService() )->setConfig($site_id, 'GROWTH_RULE', $data);         return true;     }      /**      * 获取积分规则配置      * @param int $site_id...
( this.coreConfigService ).setConfig(site_id, 'GROWTH_RULE', data);
        return true;
    }

    /**
     * 获取积分规则配置
     * @param int site_id
     * @return */
    async getPointRuleConfig(int site_id)
    {
        return ( this.coreConfigService ).getConfigValue(site_id, 'POINT_RULE');
    }

    /**
     * 配置积分规则
     * @param int site_id
     * @param data
     * @return true
     */
    async setPointRuleConfig(int site_id, data)
    {
        ( this.coreConfigService ).setConfig(site_id, 'POINT_RULE', data);
        return true;
    }

    /**
     * 获取会员提现设置
     * @param int site_id
     * @return */
    async getCashOutConfig(int site_id)
    {
        const config = ( this.coreConfigService ).getConfig(site_id, ConfigKeyDict.MEMBER_CASH_OUT)[ 'value' ] || [];
        return [
            is_open: config[ 'is_open' ] || '0',//是否启用提现
            transfer_type: config[ 'transfer_type' ] || [],//提现方式
            min: config[ 'min' ] || '0',//最低提现金额
//            max: '0',//最高提现金额
            rate: config[ 'rate' ] || '0',//手续费比率
            is_auto_verify: config[ 'is_auto_verify' ] || '0',  //是否自动审核
            is_auto_transfer: config[ 'is_auto_transfer' ] || '0',  //是否自动转账
        ];
    }

    /**
     * 会员提现配置
     * @param int site_id
     * @param data
     * @return true
     */
    async setCashOutConfig(int site_id, data)
    {
        //校验转账方式是否合法
        const transfer_type_list = array_keys(TransferDict.getTransferType()];
        if (array_diff(array_diff(data[ 'transfer_type' ), transfer_type_list), transfer_type_list)) throw new BusinessException('TRANSFER_TYPE_NOT_EXIST');
        foreach (transfer_type_list as key => item) {
            if (!in_[item, data[ 'transfer_type' ))) {
                unset(transfer_type_list[ key )];
            }
  }

  /**
   * getPointRuleConfig
   * 对应 PHP: CoreMemberConfigService_core::getPointRuleConfig()
   * 逻辑类型: undefined - undefined
   */
  async getPointRuleConfig(site_id: number) {
    // 基于PHP真实逻辑: getPointRuleConfig
    // PHP原文: return ( new CoreConfigService() )->getConfigValue($site_id, 'POINT_RULE');     }      /**      * 配置积分规则      * @param int $site_id      * @param arra...
return ( this.coreConfigService ).getConfigValue(site_id, 'POINT_RULE');
    }

    /**
     * 配置积分规则
     * @param int site_id
     * @param data
     * @return true
     */
    async setPointRuleConfig(int site_id, data)
    {
        ( this.coreConfigService ).setConfig(site_id, 'POINT_RULE', data);
        return true;
    }

    /**
     * 获取会员提现设置
     * @param int site_id
     * @return */
    async getCashOutConfig(int site_id)
    {
        const config = ( this.coreConfigService ).getConfig(site_id, ConfigKeyDict.MEMBER_CASH_OUT)[ 'value' ] || [];
        return [
            is_open: config[ 'is_open' ] || '0',//是否启用提现
            transfer_type: config[ 'transfer_type' ] || [],//提现方式
            min: config[ 'min' ] || '0',//最低提现金额
//            max: '0',//最高提现金额
            rate: config[ 'rate' ] || '0',//手续费比率
            is_auto_verify: config[ 'is_auto_verify' ] || '0',  //是否自动审核
            is_auto_transfer: config[ 'is_auto_transfer' ] || '0',  //是否自动转账
        ];
    }

    /**
     * 会员提现配置
     * @param int site_id
     * @param data
     * @return true
     */
    async setCashOutConfig(int site_id, data)
    {
        //校验转账方式是否合法
        const transfer_type_list = array_keys(TransferDict.getTransferType()];
        if (array_diff(array_diff(data[ 'transfer_type' ), transfer_type_list), transfer_type_list)) throw new BusinessException('TRANSFER_TYPE_NOT_EXIST');
        foreach (transfer_type_list as key => item) {
            if (!in_[item, data[ 'transfer_type' ))) {
                unset(transfer_type_list[ key )];
            }
  }

  /**
   * setPointRuleConfig
   * 对应 PHP: CoreMemberConfigService_core::setPointRuleConfig()
   * 逻辑类型: undefined - undefined
   */
  async setPointRuleConfig(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: setPointRuleConfig
    // PHP原文: ( new CoreConfigService() )->setConfig($site_id, 'POINT_RULE', $data);         return true;     }      /**      * 获取会员提现设置      * @param int $site_id ...
( this.coreConfigService ).setConfig(site_id, 'POINT_RULE', data);
        return true;
    }

    /**
     * 获取会员提现设置
     * @param int site_id
     * @return */
    async getCashOutConfig(int site_id)
    {
        const config = ( this.coreConfigService ).getConfig(site_id, ConfigKeyDict.MEMBER_CASH_OUT)[ 'value' ] || [];
        return [
            is_open: config[ 'is_open' ] || '0',//是否启用提现
            transfer_type: config[ 'transfer_type' ] || [],//提现方式
            min: config[ 'min' ] || '0',//最低提现金额
//            max: '0',//最高提现金额
            rate: config[ 'rate' ] || '0',//手续费比率
            is_auto_verify: config[ 'is_auto_verify' ] || '0',  //是否自动审核
            is_auto_transfer: config[ 'is_auto_transfer' ] || '0',  //是否自动转账
        ];
    }

    /**
     * 会员提现配置
     * @param int site_id
     * @param data
     * @return true
     */
    async setCashOutConfig(int site_id, data)
    {
        //校验转账方式是否合法
        const transfer_type_list = array_keys(TransferDict.getTransferType()];
        if (array_diff(array_diff(data[ 'transfer_type' ), transfer_type_list), transfer_type_list)) throw new BusinessException('TRANSFER_TYPE_NOT_EXIST');
        foreach (transfer_type_list as key => item) {
            if (!in_[item, data[ 'transfer_type' ))) {
                unset(transfer_type_list[ key )];
            }
  }

  /**
   * getCashOutConfig
   * 对应 PHP: CoreMemberConfigService_core::getCashOutConfig()
   * 逻辑类型: undefined - undefined
   */
  async getCashOutConfig(site_id: number) {
    // 基于PHP真实逻辑: getCashOutConfig
    // PHP原文: $config = ( new CoreConfigService() )->getConfig($site_id, ConfigKeyDict::MEMBER_CASH_OUT)[ 'value' ] ?? [];         return [             'is_open' =>...
const config = ( this.coreConfigService ).getConfig(site_id, ConfigKeyDict.MEMBER_CASH_OUT)[ 'value' ] || [];
        return [
            is_open: config[ 'is_open' ] || '0',//是否启用提现
            transfer_type: config[ 'transfer_type' ] || [],//提现方式
            min: config[ 'min' ] || '0',//最低提现金额
//            max: '0',//最高提现金额
            rate: config[ 'rate' ] || '0',//手续费比率
            is_auto_verify: config[ 'is_auto_verify' ] || '0',  //是否自动审核
            is_auto_transfer: config[ 'is_auto_transfer' ] || '0',  //是否自动转账
        ];
    }

    /**
     * 会员提现配置
     * @param int site_id
     * @param data
     * @return true
     */
    async setCashOutConfig(int site_id, data)
    {
        //校验转账方式是否合法
        const transfer_type_list = array_keys(TransferDict.getTransferType()];
        if (array_diff(array_diff(data[ 'transfer_type' ), transfer_type_list), transfer_type_list)) throw new BusinessException('TRANSFER_TYPE_NOT_EXIST');
        foreach (transfer_type_list as key => item) {
            if (!in_[item, data[ 'transfer_type' ))) {
                unset(transfer_type_list[ key )];
            }
  }

  /**
   * setCashOutConfig
   * 对应 PHP: CoreMemberConfigService_core::setCashOutConfig()
   * 逻辑类型: undefined - undefined
   */
  async setCashOutConfig(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: setCashOutConfig
    // PHP原文: //校验转账方式是否合法         $transfer_type_list = array_keys(TransferDict::getTransferType());         if (array_diff(array_diff($data[ 'transfer_type' ], $t...
//校验转账方式是否合法
        const transfer_type_list = array_keys(TransferDict.getTransferType()];
        if (array_diff(array_diff(data[ 'transfer_type' ), transfer_type_list), transfer_type_list)) throw new BusinessException('TRANSFER_TYPE_NOT_EXIST');
        foreach (transfer_type_list as key => item) {
            if (!in_[item, data[ 'transfer_type' ))) {
                unset(transfer_type_list[ key )];
            }
  }
}
